package com.wolf.cx330.download

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.TimeUtils
import com.wolf.cx330.R
import com.wolf.cx330.camera.CameraXAct
import com.wolf.cx330.config.Config
import com.wolf.cx330.http.ApiManager
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_download.*
import okhttp3.ResponseBody

class DownloadAct : AppCompatActivity() {
    companion object {
        fun actionStart(context: Context) {
            context.startActivity(Intent(context, DownloadAct::class.java))
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_download)
        downLoadBtn.setOnClickListener {
            ApiManager.getApi(Config.BASE_URL).downloadFile("imModel.result").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : SingleObserver<ResponseBody> {
                    override fun onSuccess(t: ResponseBody) {
//                    showScreenIv.setImageBitmap(BitmapFactory.decodeStream(t.byteStream()))
                        setResultTv("${TimeUtils.getNowString()} 请求完成 ...")
                    }

                    override fun onSubscribe(d: Disposable) {
                        setResultTv("${TimeUtils.getNowString()} 请求中 ...")
                    }

                    override fun onError(e: Throwable) {
                        setResultTv("${TimeUtils.getNowString()} ERROR ${e.message}")
                    }
                })
        }
    }

    @SuppressLint("SetTextI18n")
    fun setResultTv(msg: String) {
        resultTv.text = "${resultTv.text} \n $msg"
    }

}